{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "proper-directive",
   "metadata": {},
   "outputs": [],
   "source": [
    "import paddle\n",
    "from paddle.vision import transforms as T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "loaded-wilson",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/dandelight/.conda/envs/paddle/lib/python3.7/site-packages/ipykernel/ipkernel.py:287: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.\n",
      "  and should_run_async(code)\n"
     ]
    }
   ],
   "source": [
    "from data import COCODataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "chinese-dutch",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/dandelight/.conda/envs/paddle/lib/python3.7/site-packages/ipykernel/ipkernel.py:287: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.\n",
      "  and should_run_async(code)\n"
     ]
    }
   ],
   "source": [
    "coco_path = '/home/dandelight/coco2017'\n",
    "root = '/'.join((coco_path, 'resized2017'))\n",
    "json = '/'.join((coco_path, 'captions_train2017.json'))\n",
    "vocab_path = '/'.join((coco_path, 'vocab.pkl'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "hired-referral",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "from build_vocab import Vocabulary\n",
    "with open(vocab_path, 'rb') as f:\n",
    "    vocab = pickle.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "anticipated-thing",
   "metadata": {},
   "outputs": [],
   "source": [
    "def preprocess_fn(img):\n",
    "    transform = T.Compose([\n",
    "        T.Resize(size=(224, 224)), #把数据长宽像素调成224*224\n",
    "        T.Normalize(mean=[127.5, 127.5, 127.5], std=[127.5, 127.5, 127.5], data_format='HWC'), #标准化\n",
    "        T.Transpose(), #原始数据形状维度是HWC格式，经过Transpose，转换为CHW格式\n",
    "        ])\n",
    "    img = transform(img)\n",
    "    return paddle.to_tensor(img, dtype='float32')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "regulated-expression",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loading annotations into memory...\n",
      "Done (t=0.58s)\n",
      "creating index...\n",
      "index created!\n"
     ]
    }
   ],
   "source": [
    "coco = COCODataset(root=root, json=json, vocab=vocab, preprocess_fn=preprocess_fn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "varied-burlington",
   "metadata": {},
   "outputs": [],
   "source": [
    "img_tensor, caption_tensor = coco[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "current-rating",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 224, 224]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "img_tensor.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "modern-champion",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[13], dtype=int64, place=CPUPlace, stop_gradient=True,\n",
       "       [1 , 3 , 5 , 6 , 7 , 4 , 8 , 9 , 10, 11, 12, 13, 2 ])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "caption_tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "described-array",
   "metadata": {},
   "outputs": [],
   "source": [
    "from collate_fn import collate_fn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "double-memorabilia",
   "metadata": {},
   "outputs": [],
   "source": [
    "batch = [coco[i] for i in range(64)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "charitable-qatar",
   "metadata": {},
   "outputs": [],
   "source": [
    "images, targets, lengths = collate_fn(batch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "detected-arthur",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[64, 3, 224, 224], dtype=float32, place=CPUPlace, stop_gradient=True,\n",
       "       [[[[-0.31764707, -0.30196080, -0.29411766, ..., -0.18431373, -0.20784314, -0.23137255],\n",
       "          [-0.31764707, -0.30196080, -0.29411766, ..., -0.17647059, -0.20784314, -0.23921569],\n",
       "          [-0.31764707, -0.30196080, -0.28627452, ..., -0.16862746, -0.21568628, -0.25490198],\n",
       "          ...,\n",
       "          [-0.45098040, -0.42745098, -0.41960785, ..., -0.48235294, -0.48235294, -0.48235294],\n",
       "          [-0.45098040, -0.43529412, -0.42745098, ..., -0.48235294, -0.49019608, -0.49019608],\n",
       "          [-0.45098040, -0.43529412, -0.42745098, ..., -0.49019608, -0.49019608, -0.49019608]],\n",
       "\n",
       "         [[-0.20000000, -0.18431373, -0.17647059, ..., -0.20000000, -0.20784314, -0.23137255],\n",
       "          [-0.20000000, -0.18431373, -0.17647059, ..., -0.18431373, -0.20784314, -0.22352941],\n",
       "          [-0.20000000, -0.18431373, -0.16862746, ..., -0.17647059, -0.20000000, -0.22352941],\n",
       "          ...,\n",
       "          [-0.37254903, -0.34901962, -0.34117648, ..., -0.34117648, -0.34117648, -0.34117648],\n",
       "          [-0.37254903, -0.35686275, -0.34901962, ..., -0.33333334, -0.34117648, -0.34117648],\n",
       "          [-0.37254903, -0.35686275, -0.34901962, ..., -0.34117648, -0.34117648, -0.34117648]],\n",
       "\n",
       "         [[-0.14509805, -0.12941177, -0.12156863, ..., -0.23137255, -0.23137255, -0.24705882],\n",
       "          [-0.14509805, -0.12941177, -0.12156863, ..., -0.21568628, -0.22352941, -0.23921569],\n",
       "          [-0.14509805, -0.12941177, -0.11372549, ..., -0.19215687, -0.20000000, -0.23137255],\n",
       "          ...,\n",
       "          [-0.30196080, -0.27843139, -0.27058825, ..., -0.26274511, -0.26274511, -0.26274511],\n",
       "          [-0.30196080, -0.28627452, -0.27843139, ..., -0.27058825, -0.27843139, -0.27843139],\n",
       "          [-0.30196080, -0.28627452, -0.27843139, ..., -0.28627452, -0.28627452, -0.28627452]]],\n",
       "\n",
       "\n",
       "        [[[ 0.85098040,  0.85882354,  0.86666667, ...,  0.85882354,  0.85098040,  0.85098040],\n",
       "          [ 0.85098040,  0.86666667,  0.88235295, ...,  0.85882354,  0.85098040,  0.85098040],\n",
       "          [ 0.84313726,  0.86666667,  0.89019608, ...,  0.85882354,  0.85098040,  0.85098040],\n",
       "          ...,\n",
       "          [-0.33333334, -0.31764707, -0.30980393, ..., -0.22352941, -0.20000000, -0.15294118],\n",
       "          [-0.32549021, -0.32549021, -0.32549021, ..., -0.20000000, -0.17647059, -0.16078432],\n",
       "          [-0.30980393, -0.31764707, -0.32549021, ..., -0.19215687, -0.23137255, -0.27843139]],\n",
       "\n",
       "         [[ 0.85098040,  0.85882354,  0.86666667, ...,  0.85882354,  0.85098040,  0.85098040],\n",
       "          [ 0.85098040,  0.86666667,  0.88235295, ...,  0.85882354,  0.85098040,  0.85098040],\n",
       "          [ 0.84313726,  0.86666667,  0.89019608, ...,  0.85882354,  0.85098040,  0.85098040],\n",
       "          ...,\n",
       "          [-0.33333334, -0.31764707, -0.30980393, ..., -0.22352941, -0.20000000, -0.15294118],\n",
       "          [-0.32549021, -0.32549021, -0.32549021, ..., -0.20000000, -0.17647059, -0.16078432],\n",
       "          [-0.30980393, -0.31764707, -0.32549021, ..., -0.19215687, -0.23137255, -0.27843139]],\n",
       "\n",
       "         [[ 0.85098040,  0.85882354,  0.86666667, ...,  0.85882354,  0.85098040,  0.85098040],\n",
       "          [ 0.85098040,  0.86666667,  0.88235295, ...,  0.85882354,  0.85098040,  0.85098040],\n",
       "          [ 0.84313726,  0.86666667,  0.89019608, ...,  0.85882354,  0.85098040,  0.85098040],\n",
       "          ...,\n",
       "          [-0.33333334, -0.31764707, -0.30980393, ..., -0.22352941, -0.20000000, -0.15294118],\n",
       "          [-0.32549021, -0.32549021, -0.32549021, ..., -0.20000000, -0.17647059, -0.16078432],\n",
       "          [-0.30980393, -0.31764707, -0.32549021, ..., -0.19215687, -0.23137255, -0.27843139]]],\n",
       "\n",
       "\n",
       "        [[[-0.25490198, -0.30980393, -0.40392157, ..., -0.81176472, -0.54509807, -0.91372550],\n",
       "          [-0.27843139, -0.29411766, -0.42745098, ..., -0.81176472, -0.54509807, -0.91372550],\n",
       "          [-0.27843139, -0.26274511, -0.44313726, ..., -0.80392158, -0.53725493, -0.91372550],\n",
       "          ...,\n",
       "          [ 0.86666667,  0.88235295,  0.89019608, ..., -0.82745099, -0.49803922, -0.83529413],\n",
       "          [ 0.89803922,  0.91372550,  0.92156863, ..., -0.91372550, -0.63137257, -0.85882354],\n",
       "          [ 0.89803922,  0.90588236,  0.89803922, ..., -0.96078432, -0.68627453, -0.76470590]],\n",
       "\n",
       "         [[-0.26274511, -0.31764707, -0.41176471, ..., -0.80392158, -0.54509807, -0.91372550],\n",
       "          [-0.28627452, -0.30196080, -0.43529412, ..., -0.80392158, -0.53725493, -0.91372550],\n",
       "          [-0.28627452, -0.27058825, -0.45098040, ..., -0.79607844, -0.52941179, -0.91372550],\n",
       "          ...,\n",
       "          [ 0.84313726,  0.86666667,  0.86666667, ..., -0.81960785, -0.48235294, -0.81960785],\n",
       "          [ 0.87450981,  0.89019608,  0.89803922, ..., -0.90588236, -0.61568630, -0.84313726],\n",
       "          [ 0.88235295,  0.89019608,  0.88235295, ..., -0.95294118, -0.67058825, -0.74901962]],\n",
       "\n",
       "         [[-0.21568628, -0.27058825, -0.37254903, ..., -0.76470590, -0.50588238, -0.87450981],\n",
       "          [-0.23921569, -0.25490198, -0.39607844, ..., -0.76470590, -0.49803922, -0.87450981],\n",
       "          [-0.23921569, -0.22352941, -0.41176471, ..., -0.76470590, -0.49019608, -0.87450981],\n",
       "          ...,\n",
       "          [ 0.82745099,  0.84313726,  0.85098040, ..., -0.82745099, -0.49019608, -0.82745099],\n",
       "          [ 0.81176472,  0.82745099,  0.83529413, ..., -0.91372550, -0.62352943, -0.85098040],\n",
       "          [ 0.78039217,  0.79607844,  0.78823531, ..., -0.96078432, -0.67843139, -0.75686276]]],\n",
       "\n",
       "\n",
       "        ...,\n",
       "\n",
       "\n",
       "        [[[-0.70980394, -0.70980394, -0.71764708, ...,  0.16862746,  0.16078432,  0.14509805],\n",
       "          [-0.67843139, -0.68627453, -0.70196080, ...,  0.54509807,  0.52941179,  0.51372552],\n",
       "          [-0.63921571, -0.66274512, -0.68627453, ...,  0.63921571,  0.62352943,  0.60784316],\n",
       "          ...,\n",
       "          [-0.67058825, -0.72549021, -0.73333335, ..., -0.57647061, -0.56862748, -0.57647061],\n",
       "          [-0.65490198, -0.71764708, -0.73333335, ..., -0.58431375, -0.57647061, -0.58431375],\n",
       "          [-0.65490198, -0.71764708, -0.73333335, ..., -0.58431375, -0.58431375, -0.58431375]],\n",
       "\n",
       "         [[-0.64705884, -0.64705884, -0.65490198, ...,  0.17647059,  0.16862746,  0.15294118],\n",
       "          [-0.61568630, -0.62352943, -0.63921571, ...,  0.55294120,  0.53725493,  0.52156866],\n",
       "          [-0.58431375, -0.60784316, -0.63137257, ...,  0.64705884,  0.63137257,  0.61568630],\n",
       "          ...,\n",
       "          [-0.66274512, -0.70980394, -0.67843139, ..., -0.49803922, -0.50588238, -0.51372552],\n",
       "          [-0.65490198, -0.69411767, -0.67058825, ..., -0.50588238, -0.51372552, -0.52156866],\n",
       "          [-0.64705884, -0.69411767, -0.67058825, ..., -0.50588238, -0.51372552, -0.52156866]],\n",
       "\n",
       "         [[-0.83529413, -0.83529413, -0.83529413, ...,  0.12941177,  0.12156863,  0.10588235],\n",
       "          [-0.78823531, -0.80392158, -0.81176472, ...,  0.50588238,  0.49803922,  0.48235294],\n",
       "          [-0.73333335, -0.75686276, -0.78039217, ...,  0.60784316,  0.59215689,  0.57647061],\n",
       "          ...,\n",
       "          [-0.64705884, -0.69411767, -0.67843139, ..., -0.41960785, -0.41960785, -0.42745098],\n",
       "          [-0.63921571, -0.68627453, -0.67843139, ..., -0.42745098, -0.42745098, -0.43529412],\n",
       "          [-0.63137257, -0.68627453, -0.67843139, ..., -0.42745098, -0.43529412, -0.43529412]]],\n",
       "\n",
       "\n",
       "        [[[-0.30196080, -0.27843139, -0.23921569, ...,  0.66274512,  0.63921571,  0.63137257],\n",
       "          [-0.27843139, -0.25490198, -0.21568628, ...,  0.23137255,  0.20000000,  0.17647059],\n",
       "          [-0.23921569, -0.21568628, -0.19215687, ..., -0.03529412, -0.05098039, -0.05882353],\n",
       "          ...,\n",
       "          [-0.26274511, -0.51372552, -0.55294120, ..., -0.38823530, -0.41960785, -0.45098040],\n",
       "          [-0.35686275, -0.46666667, -0.58431375, ..., -0.36470589, -0.42745098, -0.49803922],\n",
       "          [-0.36470589, -0.36470589, -0.52941179, ..., -0.38823530, -0.49019608, -0.56862748]],\n",
       "\n",
       "         [[ 0.16078432,  0.18431373,  0.20000000, ...,  0.73333335,  0.71764708,  0.70980394],\n",
       "          [ 0.16078432,  0.18431373,  0.20000000, ...,  0.26274511,  0.23137255,  0.21568628],\n",
       "          [ 0.16078432,  0.18431373,  0.20000000, ..., -0.05882353, -0.06666667, -0.07450981],\n",
       "          ...,\n",
       "          [-0.20784314, -0.45882353, -0.49803922, ..., -0.56078434, -0.58431375, -0.61568630],\n",
       "          [-0.30196080, -0.41176471, -0.52941179, ..., -0.53725493, -0.60000002, -0.67058825],\n",
       "          [-0.30980393, -0.30980393, -0.47450981, ..., -0.56078434, -0.66274512, -0.74117649]],\n",
       "\n",
       "         [[ 0.60784316,  0.62352943,  0.63921571, ...,  0.51372552,  0.49019608,  0.47450981],\n",
       "          [ 0.58431375,  0.60784316,  0.63137257, ...,  0.06666667,  0.02745098, -0.00392157],\n",
       "          [ 0.55294120,  0.57647061,  0.60784316, ..., -0.23921569, -0.24705882, -0.26274511],\n",
       "          ...,\n",
       "          [-0.12941177, -0.38039216, -0.41960785, ..., -0.89019608, -0.91372550, -0.93725491],\n",
       "          [-0.22352941, -0.34117648, -0.45882353, ..., -0.89803922, -0.96078432, -0.98431373],\n",
       "          [-0.23137255, -0.23921569, -0.41176471, ..., -0.94509804, -0.99215686, -1.        ]]],\n",
       "\n",
       "\n",
       "        [[[ 0.46666667,  0.45882353,  0.46666667, ..., -0.10588235,  0.15294118,  0.34901962],\n",
       "          [ 0.46666667,  0.46666667,  0.47450981, ..., -0.10588235,  0.14509805,  0.33333334],\n",
       "          [ 0.47450981,  0.46666667,  0.48235294, ..., -0.11372549,  0.13725491,  0.31764707],\n",
       "          ...,\n",
       "          [ 0.01960784,  0.02745098,  0.03529412, ...,  0.24705882,  0.20000000,  0.16862746],\n",
       "          [ 0.01960784,  0.02745098,  0.03529412, ...,  0.25490198,  0.20000000,  0.16078432],\n",
       "          [ 0.01960784,  0.02745098,  0.02745098, ...,  0.25490198,  0.19215687,  0.16078432]],\n",
       "\n",
       "         [[ 0.19215687,  0.18431373,  0.19215687, ..., -0.38823530, -0.09803922,  0.12156863],\n",
       "          [ 0.19215687,  0.19215687,  0.20000000, ..., -0.39607844, -0.10588235,  0.10588235],\n",
       "          [ 0.20000000,  0.19215687,  0.20784314, ..., -0.39607844, -0.11372549,  0.09019608],\n",
       "          ...,\n",
       "          [-0.14509805, -0.13725491, -0.12941177, ..., -0.09019608, -0.09019608, -0.09019608],\n",
       "          [-0.14509805, -0.13725491, -0.12941177, ..., -0.09803922, -0.09803922, -0.09803922],\n",
       "          [-0.14509805, -0.13725491, -0.13725491, ..., -0.09803922, -0.10588235, -0.10588235]],\n",
       "\n",
       "         [[-0.10588235, -0.12156863, -0.12156863, ..., -0.75686276, -0.60784316, -0.49803922],\n",
       "          [-0.10588235, -0.11372549, -0.11372549, ..., -0.75686276, -0.61568630, -0.51372552],\n",
       "          [-0.09803922, -0.10588235, -0.10588235, ..., -0.76470590, -0.62352943, -0.52941179],\n",
       "          ...,\n",
       "          [-0.56078434, -0.55294120, -0.54509807, ..., -0.31764707, -0.25490198, -0.19215687],\n",
       "          [-0.56078434, -0.55294120, -0.54509807, ..., -0.32549021, -0.24705882, -0.19215687],\n",
       "          [-0.56078434, -0.55294120, -0.55294120, ..., -0.32549021, -0.24705882, -0.18431373]]]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "searching-iceland",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[64, 20], dtype=int64, place=CPUPlace, stop_gradient=True,\n",
       "       [[1  , 3  , 37 , ..., 50 , 13 , 2  ],\n",
       "        [1  , 3  , 115, ..., 13 , 2  , 0  ],\n",
       "        [1  , 3  , 197, ..., 13 , 2  , 0  ],\n",
       "        ...,\n",
       "        [1  , 3  , 208, ..., 0  , 0  , 0  ],\n",
       "        [1  , 3  , 47 , ..., 0  , 0  , 0  ],\n",
       "        [1  , 3  , 115, ..., 0  , 0  , 0  ]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "targets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "outstanding-progressive",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[20,\n",
       " 19,\n",
       " 19,\n",
       " 17,\n",
       " 16,\n",
       " 16,\n",
       " 16,\n",
       " 15,\n",
       " 15,\n",
       " 15,\n",
       " 15,\n",
       " 15,\n",
       " 14,\n",
       " 14,\n",
       " 14,\n",
       " 14,\n",
       " 14,\n",
       " 14,\n",
       " 14,\n",
       " 14,\n",
       " 14,\n",
       " 13,\n",
       " 13,\n",
       " 13,\n",
       " 13,\n",
       " 13,\n",
       " 13,\n",
       " 13,\n",
       " 12,\n",
       " 12,\n",
       " 12,\n",
       " 12,\n",
       " 12,\n",
       " 12,\n",
       " 12,\n",
       " 12,\n",
       " 12,\n",
       " 12,\n",
       " 12,\n",
       " 12,\n",
       " 12,\n",
       " 12,\n",
       " 12,\n",
       " 12,\n",
       " 11,\n",
       " 11,\n",
       " 11,\n",
       " 11,\n",
       " 11,\n",
       " 11,\n",
       " 11,\n",
       " 11,\n",
       " 11,\n",
       " 11,\n",
       " 11,\n",
       " 11,\n",
       " 11,\n",
       " 11,\n",
       " 11,\n",
       " 10,\n",
       " 10,\n",
       " 10,\n",
       " 10,\n",
       " 10]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lengths"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
